package top.lixunda.ecommerce.server.good.api.entity.vo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.good.api.entity.db.GoodGroupDO;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 树形结构的GoodGroupVO
 *
 * @author Xumda
 * @version time:2020/2/6 22:25
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
public class GoodGroupTreeVO {

    /**
     * 当前节点
     */
    private GoodGroupVO node;

    /**
     * 子节点
     */
    private List<GoodGroupTreeVO> children;

    /**
     * 转换
     *
     * @param list 带转换数据
     * @return 转换后的数据
     */
    public static GoodGroupTreeVO fromGoodGroupDoList(List<GoodGroupDO> list) {
        GoodGroupTreeVO treeVO = new GoodGroupTreeVO();
        if (ObjectValidators.isEmpty(list)) {
            return treeVO;
        }
        // 移除空项
        list = list.stream().filter(Objects::nonNull).collect(Collectors.toList());
        // 获取根节点
        List<GoodGroupDO> rootList = list.stream()
                .filter(item -> GoodGroupDO.ROOT_IS.equals(item.getIsRoot()))
                .sorted(Comparator.comparing(GoodGroupDO::getName))
                .collect(Collectors.toList());
        // 移除根节点
        list.removeAll(rootList);
        treeVO.setChildren(
                rootList.stream()
                        .map(item -> {
                            GoodGroupTreeVO vo = new GoodGroupTreeVO();
                            vo.setNode(GoodGroupVO.fromGoodGroupDO(item));
                            return vo;
                        })
                        .collect(Collectors.toList()));

        if (ObjectValidators.isEmpty(list) || ObjectValidators.isEmpty(treeVO.getChildren())) {
            return treeVO;
        }

        // 递归
        for (GoodGroupTreeVO vo : treeVO.getChildren()) {
            addChildren(vo, list);
        }

        return treeVO;
    }

    private static void addChildren(GoodGroupTreeVO goodGroupTreeVO,
                                    List<GoodGroupDO> list) {
        if (ObjectValidators.isEmpty(list)) {
            return;
        }
        List<GoodGroupDO> tempList = new ArrayList<>(list.size());
        for (GoodGroupDO goodGroupDO : list) {
            if (goodGroupDO.getParent().equals(goodGroupTreeVO.getNode().getId())) {
                tempList.add(goodGroupDO);
            }
        }
        list.removeAll(tempList);
        goodGroupTreeVO.setChildren(tempList.stream()
                .map(item -> {
                    GoodGroupTreeVO vo = new GoodGroupTreeVO();
                    vo.setNode(GoodGroupVO.fromGoodGroupDO(item));
                    return vo;
                })
                .collect(Collectors.toList()));

        if (ObjectValidators.isEmpty(goodGroupTreeVO.getChildren()) || list.size() == 0) {
            return;
        }

        // 递归
        for (GoodGroupTreeVO vo : goodGroupTreeVO.getChildren()) {
            // 叶节点，跳过
            if (GoodGroupDO.ROOT_IS.equals(vo.getNode().getIsLeaf())) {
                continue;
            }
            addChildren(vo, list);
        }
    }

}
